import DatetimeView from "vux/src/components/datetime-view/index.vue";
import dateFormat from "vux/src/tools/date/format.js";

import template from "./dataTimeFilterTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    DatetimeView,
  },
  props: {
    beginTime: {
      //数据
      type: String,
      default: dateFormat(new Date(), "YYYY-MM-DD"),
    },
    endTime: {
      //数据
      type: String,
      default: dateFormat(new Date(), "YYYY-MM-DD"),
    },
    datePicker: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      value1: dateFormat(new Date(), "YYYY-MM-DD"),
      value2: dateFormat(new Date(), "YYYY-MM-DD"),
      maxDay: "",
    };
  },
  created() {},
  methods: {
    backTo() {
      this.value1 = this.beginTime || dateFormat(new Date(), "YYYY-MM-DD");
      this.value2 = this.endTime || dateFormat(new Date(), "YYYY-MM-DD");
      this.$emit("pickHide", false);
    },
    timeSure() {
      let vm = this;
      if (vm.value1 > vm.value2) {
        vm.$vux.toast.show({
          text: "结束时间不能小于开始时间",
        });
        return false;
      }
      let end = new Date(vm.value2);
      let now = new Date();
      if (end > now) {
        vm.$vux.toast.show({
          text: "结束时间不能大于当前时间",
        });
        return false;
      }
      vm._getReportMaxDate();
    },
    _getReportMaxDate() {
      let vm = this;
      vm.$vux.loading.show();
      vm.$http
        .post(vm.urlRequest + "?m=api&c=user&a=getReportMaxDate")
        .then((res) => {
          vm.$vux.loading.hide();
          if (res.status == 0) {
            let begin = new Date(vm.value1);
            let end = new Date(vm.value2);
            let differDays =
              parseInt(Math.abs(end - begin) / 1000 / 3600 / 24) + 1;
            vm.maxDay = res.data.maxDay;
            if (vm.maxDay && differDays > vm.maxDay) {
              vm.$vux.toast.show({
                text: "所选日期跨度不能超过" + vm.maxDay + "天",
              });
              return false;
            }
            vm.$emit("getTime", [vm.value1, vm.value2]);
          }
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          vm.$vux.toast.show({
            text: "数据请求超时",
          });
          console.log(err);
        });
    },
  },
  watch: {
    beginTime() {
      if (this.beginTime && this.beginTime != "") {
        this.value1 = this.beginTime;
      } else {
        this.value1 = dateFormat(new Date(), "YYYY-MM-DD");
      }
    },
    endTime() {
      if (this.endTime && this.endTime != "") {
        this.value2 = this.endTime;
      } else {
        this.value2 = dateFormat(new Date(), "YYYY-MM-DD");
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/common/dataTimeFilter/dataTimeFilter.vue
